/**
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */

package com.poltman.dscentral.system;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;


import com.poltman.dscentral.core.CoreStatics;
import com.poltman.dscentral.core.webappinit.WebAppInit;

/**
 * 
 * @author z.ciok@poltman.com
 *
 */
@SuppressWarnings("rawtypes")
public class SaveAuthenticationStack extends SelectorComposer  {

	private static final long serialVersionUID = -8555846998435924917L;

	private static Logger log = Logger.getLogger(SaveAuthenticationStack.class);
	
	@Wire
	private Listbox authenticationTargetListbox;
	
	/**
	 * 
	 */
	@Listen("onClick = button#saveAuthenticationStackBtn")
	public void saveAuthenticationStack() {
		
		Map<String, String> namePluginMap = new HashMap<String, String>();
		namePluginMap.put(CoreStatics.IP, CoreStatics.ORG_DSPACE_AUTHENTICATE_IPAUTHENTICATION);
		namePluginMap.put(CoreStatics.LDAP, CoreStatics.ORG_DSPACE_AUTHENTICATE_LDAPAUTHENTICATION);
		namePluginMap.put(CoreStatics.HLDAP, CoreStatics.ORG_DSPACE_AUTHENTICATE_LDAPHIERARCHICALAUTHENTICATION);
		namePluginMap.put(CoreStatics.PASSWORD, CoreStatics.ORG_DSPACE_AUTHENTICATE_PASSWORDAUTHENTICATION);
		namePluginMap.put(CoreStatics.SHIBBOLETH, CoreStatics.ORG_DSPACE_AUTHENTICATE_SHIBAUTHENTICATION);
		namePluginMap.put(CoreStatics.X509, CoreStatics.ORG_DSPACE_AUTHENTICATE_X509AUTHENTICATION);		
		
		try {
			String dspaceAuthenticationFilePath = 
					WebAppInit.getInstance().getDspaceDir() + File.separator + "config" + 
					File.separator + "modules" + 
					File.separator + "authentication.cfg";
			Properties pro = new Properties();		
			FileInputStream in = new FileInputStream(dspaceAuthenticationFilePath);
			pro.load(in);
			//String authenticationMethod = pro.getProperty("plugin.sequence.org.dspace.authenticate.AuthenticationMethod");
			
			List<Component> items = authenticationTargetListbox.getChildren();
			if (items.size() < 1) {
				//TODO: i18n
				Messagebox.show("Nothing to save error...", "DSCentral Error", 0,  Messagebox.ERROR);
				return;
			}
			Listitem item = null;
			String value = "";
			for (int i = 0; i < items.size(); i++) {
				item = ((Listitem)items.get(i));
				value = value + ((value == "") ? "" : ", ") + namePluginMap.get(item.getLabel());
			}
			pro.setProperty("plugin.sequence.org.dspace.authenticate.AuthenticationMethod", value);
			pro.store(new FileOutputStream(dspaceAuthenticationFilePath), null);
		} catch (Exception e) {
			log.error("Error: saveAuthenticationStack...; ", e);
			//TODO: i18n
			Messagebox.show("Authentication Stack Save Error...", "DSCentral Error", 0,  Messagebox.ERROR);
			return;
		}
		
		//TODO: i18n
		Messagebox.show("Authentication Stack has been saved...", "DSCentral Error", 0,  Messagebox.INFORMATION);
	}	
	
}
